/**
 * Created by Jaye on 2017/12/29.
 */
var addApiPackage = [];
$(function () {
    initTenantApiPackageTable();

    /**
     * 接入点模态框
     */
    $('#accessPointModal').on('show.bs.modal', function () {
        $("#accessPointTable").bootstrapTable('destroy');
        $("#accessPointTable").bootstrapTable({
            url: "/accesspoint/list",
            method: "post",
            height: "300",
            iconSize: "outline",
            search: true,
            showRefresh: true,
            showColumns: !0,
            singleSeclect: true,
            columns: [
                {radio: true},
                {
                    title: "接入点Id",
                    field: "accessPointId",
                    width: "300",
                    align: "left"
                }, {
                    title: "接入点名称",
                    field: "accessPointUserName",
                    width: "300",
                    align: "center"
                }, {
                    title: "接入点昵称",
                    field: "accessPointNickName",
                    width: "300",
                    align: "center"
                }],
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list"
            }
        });
    });
    /**添加服务包监听
     * batchAddApiPackageButton
     */
    $('#batchAddApiPackageButton').on('click', function () {
        $('#apiPackageModal').modal('show');
    });
    $('#apiPackageModal').on('show.bs.modal', function () {
        $("#apiPackageTable").bootstrapTable('destroy');
        $("#apiPackageTable").bootstrapTable({
            url: "/apipackage/list/all",
            method: "post",
            height: "300",
            iconSize: "outline",
            search: true,
            showRefresh: true,
            showColumns: !0,
            singleSeclect: true,
            columns: [
                {checkbox: true},
                {
                    title: "服务包Id",
                    field: "apiPackageId",
                    align: "center"
                }, {
                    title: "服务包名称",
                    field: "apiPackageName",
                    align: "center"
                },
                {
                    title: "有效时长(s)",
                    field: "term",
                    align: "center"
                },
                {
                    title: "价格(元)",
                    field: "price",
                    align: "center"
                }],
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list"
            }
        });
    });
    $('#settingPackageModal').on('show.bs.modal', function () {
        addApiPackage = [];
        $('#accesspoint_id').val("");
        $('#accesspoint_name').val("");
        var data = "";
        $("#addApiPackageTable").bootstrapTable('destroy');
        $("#addApiPackageTable").bootstrapTable({
            data: data,
            height: "200",
            iconSize: "outline",
            search: true,
            showRefresh: true,
            showColumns: !0,
            singleSeclect: true,
            columns: [
                {radio: true},
                {
                    title: "服务包Id",
                    field: "apiPackageId",
                    align: "center"
                }, {
                    title: "服务包名称",
                    field: "apiPackageName",
                    align: "center"
                },
                {
                    title: "有效时长(s)",
                    field: "term",
                    align: "center"
                },
                {
                    title: "价格(元)",
                    field: "price",
                    align: "center"
                }],
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list"
            }
        });
    });
});
/**
 * 初始化租户服务包列表
 */
function initTenantApiPackageTable() {
    $('#tenantApiPackageTable').bootstrapTable({
        url: "/tenant/apipackage/list",
        method: "post",
        height: "550",
        search: true,
        iconSize: "outline",
        pagination: true,
        showRefresh: true,
        showColumns: !0,
        columns: [
            // {checkbox: true},
            {
                title: "接入点服务包Id",
                field: "accessPointApiPackageId",
                align: "center",
                visible: false
            },
            {
                title: "接入点编号",
                field: "tenantId",
                // width: "300",
                align: "center"
            }, {
                title: "服务包Id",
                field: "apiPackageId",
                // width: "300",
                align: "center"
            }, {
                title: "服务包名称",
                field: "apiPackageName",
                // width: "300",
                align: "center"
            }, {
                title: "操作",
                field: "operation",
                // width: "300",
                align: "center",
                formatter: function (value) {
                    // if (value == 1) {
                    //     return "<span type='text' class='btn btn-primary btn-sm'>已启用</span>";
                    // } else {
                    //     return "<span type='text' class='btn btn-danger btn-sm'>已禁用</span>";
                    // }
                    return "<span type='text' class='btn btn-warning btn-sm'>修改</span> " +
                        "<span type='text' class='btn btn-danger btn-sm'>删除</span>";
                }
            }],
        icons: {
            refresh: "glyphicon-repeat",
            toggle: "glyphicon-list-alt",
            columns: "glyphicon-list"
        }
    });
}

function selectAccessPoint() {
    var selectContent = $("#accessPointTable").bootstrapTable('getSelections')[0];
    if (typeof(selectContent) == 'undefined') {
        tip_noselect();
        return false;
    } else {
        // console.info(selectContent);
        $("#accesspoint_id").val(selectContent.accessPointId);
        $("#accesspoint_name").val(selectContent.accessPointUserName);
        $('#accessPointModal').modal('hide');
        return;
    }
}
function tip_noselect() {
    swal({
        title: "请选择一列数据",
        type: "warning",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        closeOnConfirm: false
    });
}
function selectApiPackage() {
    var selectContent = $("#apiPackageTable").bootstrapTable('getSelections');
    if (typeof(selectContent) == 'undefined' || selectContent.length < 1) {
        tip_noselect();
        return false;
    } else {
        // $("#addApisTable").bootstrapTable("append", selectContent);
        for (var i = 0; i < selectContent.length; i++) {
            // alert(selectContent[i].apiId);
            addApiPackageRow(selectContent[i]);
        }
        $("#apiPackageModal").modal('hide');
    }
}
function addApiPackageRow(data) {
    var havedData = $("#addApiPackageTable").bootstrapTable('getData');
    var havedDataLength = $("#addApiPackageTable").bootstrapTable('getData').length;
    var index = $("#apiPackageTable").bootstrapTable('getData').length;

    if (havedDataLength > 0) {
        for (var i = 0; i < havedDataLength; i++) {
            if (havedData[i].apiPackageId == data.apiPackageId) {
                swal({
                    title: "Warning",
                    text: "部分数据已经存在，无需重复添加！",
                    type: "warning",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
                return;
            }
        }
    }
    $("#addApiPackageTable").bootstrapTable('insertRow', {
        index: index,
        row: {
            apiPackageId: data.apiPackageId,
            apiPackageName: data.apiPackageName,
            term: data.term,
            price: data.price
        }
    });
    addApiPackage.push(data.apiPackageId);
}
function saveSetting() {
    var accessPointId = $("#accesspoint_id").val();
    if (addApiPackage.length > 0) {

        var json = new ChangeTenantApiPacge(accessPointId, addApiPackage);
        $.ajax({
            url: "/tenant/apipackage/bind",
            type: "post",
            data: JSON.stringify(json),
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            dataType: "json",
            beforeSend: function () {
                // uploading = true;
            },
            success: function (data) {
                if (data.code == 1000) {
                    swal({
                        title: "保存成功",
                        type: "success",
                        confirmButtonText: "确认",
                        closeOnConfirm: false
                    }, function () {
                        $('#settingPackageModal').modal('hide');
                        location.reload();
                    });
                } else {
                    swal({
                        title: "保存失败",
                        type: "error",
                        buttons: false,
                        timer: 1500
                    });
                    return;
                }

                // location.reload();
            },
            error: function (returndata) {
                alert(returndata);
                $('#settingPackageModal').modal('hide');
            }
        });

    } else {
        swal({
            title: "没有变更数据",
            type: "warning",
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            closeOnConfirm: false
        });
    }
}
function ChangeTenantApiPacge(accessPointId, addApiPackages) {
    this.accessPointId = accessPointId;
    this.addApiPackages = addApiPackages;
}